<template>
  <div id="Home">
    <el-container class="Home-container">
      <el-header class="Home-Header">
        <div class="Home-Logo">
          <div class="logo-icon">
            <el-icon><ScaleToOriginal /></el-icon>
          </div>
          <span class="logo-text">在线法律支援平台</span>
        </div>
        <el-menu
          class="Home-Nav-Menu"
          mode="horizontal"
          :ellipsis="false"
          :router="true"
          text-color="#e6e6e6"
          active-text-color="#FFD700"
          :default-active="activeIndex"
        >
          <el-menu-item index="/">首页</el-menu-item>
          <el-menu-item index="/consultation">法律咨询</el-menu-item>
          <el-menu-item index="/lawyers">律师团队</el-menu-item>
          <el-menu-item index="4">社区互助</el-menu-item>
          <el-menu-item index="5">技术支持</el-menu-item>
          <el-menu-item index="/document-templates">表单编辑</el-menu-item>
        </el-menu>
        <div class="Home-User-Content">
          <div v-if="!userStore.isLoggedIn" class="auth-buttons">
            <el-button @click="router.push('/login')" type="primary" class="login-btn"
              ><el-icon><User /></el-icon>登录</el-button
            ><el-button @click="router.push('/login')" type="primary" class="register-btn"
              >注册<el-icon><Plus /></el-icon
            ></el-button>
          </div>
          <div v-else class="user-info">
            <el-dropdown placement="bottom">
              <span class="el-dropdown-link">
                <el-avatar :size="40" :src="userStore.user?.avatar" class="user-avatar" />
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item>
                    <span class="username">{{ userStore.displayName }}</span>
                  </el-dropdown-item>
                  <el-dropdown-item>
                    <el-icon><User /></el-icon>个人中心
                  </el-dropdown-item>
                  <el-dropdown-item>
                    <el-icon><Setting /></el-icon>账号设置
                  </el-dropdown-item>
                  <el-dropdown-item divided @click="handleLogout">
                    <el-icon><SwitchButton /></el-icon>退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>
      <el-main class="Home-Main"><RouterView></RouterView></el-main>
      <el-footer class="Home-Footer">
        <div class="footer-content">
          <div class="footer-links">
            <a href="#">服务条款</a>
            <a href="#">隐私政策</a>
            <a href="#">联系我们</a>
            <a href="#">帮助中心</a>
          </div>
          <div class="copyright">© 2025 在线法律支援平台 | 版权所有</div>
        </div></el-footer
      >
    </el-container>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { ref, reactive, onMounted } from 'vue'
import { userInfoStore } from '@/stores/userinfo'
import { ElMessage, ElMessageBox } from 'element-plus'
import { User, ScaleToOriginal, Plus, Setting, SwitchButton } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = userInfoStore()

const activeIndex = ref('/')

const handleLogout = async () => {
  try {
    await userStore.logout()

    ElMessage.info('登出成功')
  } catch (error) {
    ElMessage.error('登出失败')
  }
}
</script>

<style scoped>
.Home-container {
  min-height: 100vh;
}
.Home-Header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  background-color: #003366;
}

.Home-Logo {
  display: flex;
  align-items: center;
  color: #ffd700;
  font-weight: bold;
  font-size: 20px;
  margin-left: 30px;
}

.logo-icon {
  margin-right: 10px;
  font-size: 24px;
}

.logo-text {
  background: linear-gradient(to right, #ffd700, #d4af37);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  font-weight: 700;
  font-size: 1.4rem;
}

.Home-Nav-Menu {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  background-color: transparent;
  border: none;
}

/* 去除下划线 */
.Home-Nav-Menu .el-menu-item {
  height: 60px;
  line-height: 60px;
  font-size: 16px;
  border-bottom: none !important;
}

.Home-Nav-Menu .el-menu-item:not(.is-active):hover {
  background-color: rgba(255, 255, 255, 0.1);
}

/* 去除激活状态下的下划线 */
.Home-Nav-Menu .el-menu-item.is-active {
  border-bottom: none !important;
  color: #d4af37 !important;
  background-color: rgba(255, 255, 255, 0.05);
}

.Home-User-Content {
  margin-right: 30px;
}

.auth-buttons {
  display: flex;
  gap: 10px;
}

.login-btn,
.register-btn {
  background-color: #d4af37;
  border-color: #d4af37;
  color: #003366;
  font-weight: bold;
}

.login-btn:hover,
.register-btn:hover {
  background-color: #c19b2e;
  border-color: #c19b2e;
}

.user-info {
  margin-right: 30px;
  display: flex;
  align-items: center;
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  color: #d4af37;
  cursor: pointer;
}

.username {
  align-items: center;
  margin: 0 8px 0 12px;
  font-weight: bold;
}

.Home-Main {
  background-color: #d7dbdb;
}

.Home-Footer {
  background: linear-gradient(135deg, #1a3a5f 0%, #2c5282 100%);
  color: #e6e6e6;
  padding: 20px 0;
}

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.footer-links {
  display: flex;
  gap: 24px;
}

.footer-links a {
  color: #e6e6e6;
  text-decoration: none;
  transition: color 0.3s;
}

.footer-links a:hover {
  color: #ffd700;
}

.copyright {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.7);
}
</style>
